﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using YuuzleComic.DBUtility;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class yzadmin_data_FrendLink : YuuzleComic.Web.UI.ManagePage
{
    protected string json = "";
    YuuzleComic.BLL.link linkbll = new YuuzleComic.BLL.link();
    private JObject result = new JObject();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request["a"])
            {
                case "list":
                    this.ProcessList();
                    break;
                case "updatecolumn":
                    this.ProcessUpdateColumn();
                    break;
                case "delete":
                    this.ProcessDelete();
                    break;
                case "save":
                    this.ProcessSave();
                    break;
                case "validateURL":
                    this.validateURL();
                    break;
                case "sort":
                    this.ProcessSort();
                    break;
                default:
                    break;
            }
        }
    }

    private void ProcessSort()
    {
        try
        {
            string sql = "";
            JArray data = JArray.Parse(Request.Form["data"]);
            foreach (JObject item in data)
            {
                sql += "update link set sitename='" + item["sitename"] + "',siteurl='" + item["siteurl"] + "',siteinfo='" + item["siteinfo"] + "' where id=" + item["id"];
            }
            DbHelperSQL.ExecuteSql(sql);
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally
        {
            json = result.ToString();
        }
    }

    private void ProcessSave()
    {
        
        try
        {
            YuuzleComic.Model.link link = new YuuzleComic.Model.link();
            link.siteinfo = Request.Form["siteinfo"].Trim();
            link.sitename = Request.Form["sitename"].Trim();
            link.siteurl = Request.Form["siteurl"].Trim();
            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                 linkbll.Add(link);
            }
            else
            {
                link.id = Convert.ToInt32(Request.Form["id"]);
                linkbll.Update(link);
            }
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally {
            json = result.ToString();
        }
    }

    private void ProcessDelete()
    {
        try
        {
            DbHelperSQL.ExecuteSql("delete from link where id in (" + Request["ids"] + ")");
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("message", e.Message);
        }
        json = result.ToString();
    }

    private void ProcessUpdateColumn()
    {
        try
        {
            DbHelperSQL.ExecuteSql("update link set " + Request["column"] + "=" + Request["value"] + " where id=" + Request["id"]);
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("message", e.Message);
        }
        json = result.ToString();
    }

    private void ProcessUpdateTJ()
    {
        throw new NotImplementedException();
    }

    private void ProcessList()
    {
        try
        {
            DataSet ds = linkbll.GetList("1=1 order by id");
            result.Add("data", JArray.FromObject(ds.Tables[0]));
            json = result.ToString();
        }
        catch (Exception)
        {
            throw;
        }
    }

    //验证URL有效性
    private void validateURL()
    {
        string url = Request["url"];
        try
        {
            System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            myRequest.Method = "HEAD";
            myRequest.Timeout = 8000;
            System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)myRequest.GetResponse();
            result.Add("success", res.StatusCode == System.Net.HttpStatusCode.OK);
            myRequest.Abort();
            res.Close();
        }
        catch (Exception e)
        {
            result.Add("success", e.Message);
        }
        json = result.ToString();
    }
}

